/*
APPENDING WAGE SURVEY

Creator		: Jonas Cederlof (JC)
Description 	: Appending the wage survey (lonestrukturstatistiken) from 
		  years 2000-2018
	
Notes		: 
LATEST UPDATE 	: 2021-10-06
*/

********************************************************************************
clear
set more		 off
cap   log close 	_all

log using 	"../log/A0_append_wagesurvey.log", replace 

{ // Append data for year 2000-2018
*===============================================================================
*Load year 2000 for all 4 sectors
use 		"$rawdatapath/wages_kommun_2000.dta"
gen sector 	= 1

append using	"$rawdatapath/wages_landsting_2000.dta" ,force
replace sector = 2 if sector==.

append using	"$rawdatapath/wages_statlig_2000.dta"
replace sector = 3 if sector==.

append using	"$rawdatapath/wages_privat_2000.dta"
replace sector = 4 if sector==.


*Append year 2001-2018 for all 4 sectors
forvalues y=2001(1)2019{
	append using 	"$rawdatapath/wages_kommun_`y'.dta"	, force
	replace sector = 1 if sector==.
	append using	"$rawdatapath/wages_landsting_`y'.dta"	, force
	replace sector = 2 if sector==.
	append using	"$rawdatapath/wages_statlig_`y'.dta"	, force
	replace sector = 3 if sector==.
	append using	"$rawdatapath/wages_privat_`y'.dta"	, force
	replace sector = 4 if sector==.
}
}
*

{ // Handle duplicates
*===============================================================================
*Note: 	There are some duplicates (Although, I think David has already fixed most 
*	of them before this). These duplciates soley come from workers being
*	registered in both sector 3 (government) and sector 4 (private) at the 
*	same time - from the same company (how that is now possible...?)!
*	I given priority to the private industry (wages are mostly the same
*	so should not matter, but the only benefit is that we get the variable
*	indicating bule and whilte-collar which is only reported for sector 4)
gduplicates tag lopnr lopnr_peorgnr  year, gen(dup)
*gduplicates report lopnr lopnr_peorgnr  year sector
drop if dup==1 & sector==3
*gduplicates report lopnr lopnr_peorgnr  year 
drop dup

*All good now!
}
*

{ // Merge on older leverans of wage survey which contain BC and WC indicator
*===============================================================================
preserve
	use  lopnr lopnr_peorgnr   manl sekt using "$rawdatapath/wages_orig_2000.dta",clear
	gen year = 2000
	forvalues i = 2001(1)2013 {
		append using "$rawdatapath/wages_orig_`i'.dta", force keep(lopnr lopnr_peorgnr   manl sekt)
		replace year = `i' if year==.
	}
	
	// Handle duplicates
	*===============================================================================
	*Note: 	These duplicates do not come from different sectors (see sekt variable)
	*	and duplicates often have different wages. I keep the highest wage
	*gduplicates report lopnr lopnr_peorgnr  year

	sort lopnr lopnr_peorgnr year manl   
	fcollapse (last)  manl sekt, by(lopnr lopnr_peorgnr year) 


	tempfile temp
	save `temp'
restore

*Get sekt-variable for years 2014-2018
preserve
	use sekt lopnr peorglopnr manl  sekt using "$rawdatapath/ArbTjm_2014.dta",clear
	gen year = 2014
	forvalues i = 2015(1)2017{
		append using "$rawdatapath/ArbTjm_`i'.dta", keep(sekt lopnr peorglopnr )
		replace year=`i' if year==.
	}	

	// Handle duplicates
	*===============================================================================
	*Note: 	These duplicates do not come from different sectors (see sekt variable)
	*	and duplicates often have different wages. I keep the highest wage
	*gduplicates report lopnr peorglopnr  year

	sort lopnr peorglopnr year manl   
	fcollapse (last) sekt, by(lopnr peorglopnr year) 

	rename peorglopnr lopnr_peorgnr
	tempfile temp2
	save `temp2'	
	
restore

merge 1:1 lopnr lopnr_peorgnr year using `temp', update
drop if _merge==2
drop _merge

merge 1:1 lopnr lopnr_peorgnr year using `temp2', update
drop if _merge==2
drop _merge


}
*



*Save data (uniqe at persid/firmid/year)
gisid lopnr lopnr_peorgnr year
drop kon stratum  sni2007 nuts
sort lopnr year lopnr_peorgnr 
order lopnr lopnr_peorgnr year manl *lon rortup skift sector sekt
compress
save "$datapath/A0_wagesurvey_00_19.dta",replace	


